package andev.schoolhelper.course;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.util.Log;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;

import java.util.Random;

public class Course_Card extends TextView {
    private int color = Color.TRANSPARENT;
    private GradientDrawable drawable;

    public Course_Card(Context context, String text) {
        super(context);
        setGravity(Gravity.CENTER);
        setTextSize(10);
        setTextColor(Color.WHITE);
        setText(text);
        drawable = new GradientDrawable();
        drawable.setCornerRadius(8);
        setClickable(true);
    }

    public void setCourseColor(int color) {
        if (color == -1) {
            setTextColor(Color.parseColor("#bfc2c5"));
        }
        this.color = color = generateColor(color);
        setCourseColor_Temp(color);

        setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        setCourseColor_Temp(Color.parseColor("#80d6d9db"));
                        break;
                    case MotionEvent.ACTION_MOVE:
                    case MotionEvent.ACTION_UP:
                        setCourseColor_Temp(Course_Card.this.color);
                        Log.i("", Course_Card.this.color + "Course_Card.this.color");
                        break;
                }
                return true;
            }
        });
    }

    public void setCourseColor_Temp(int color) {
        drawable.setColor(color);
        //drawable.setShape(GradientDrawable.OVAL);
        setBackground(drawable);
    }

    private int generateColor(int color2) {
        if (color2 == -1)//-1为不用上课
        {
            return Color.parseColor("#80e2e3e4");
        }
        if (color2 != 1)//1为允许自动生成颜色
        {
            return color2;
        }
        StringBuffer color = new StringBuffer("#d0");
        String[] colorbase = {
                "00868B",
                //"FFDAB9",
                "43CD80",
                "6495ED",
                //"FFF68F",
                //"FFEC8B",
                "CD5C5C",
                "8B658B",
                "D2691E",
                "FF1493",
                "EE2C2C",
                "FF4500",
                "008B8B",
                "8B008B",
                "68228B",
                "1E90FF"};
        Random random = new Random();
        int i = random.nextInt(colorbase.length);
        color.append(colorbase[i]);
        return Color.parseColor(color.toString());
    }

}
